<?php
class Module
{
	public $Table="module";
	public $ModuleID;
	public $ModuleName;
	public $ModuleParent;
	public $Pos;
	
	function loadByKey($id)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where ModuleID='".$id."'";
		$row=$ob->singleData($sql);
		$this->ModuleID=$row['ModuleID'];
		$this->ModuleName=$row['ModuleName'];
		$this->ModuleParent=$row['ModuleParent'];
		$this->Pos=$row['Pos'];
	}
	function getTable($dk)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` $dk";
		$data=$ob->mutiData($sql);
		return $data;
	}
	function insert($conn)
	{
		$ob=new ClassConnect();
		$field=array("ModuleID","ModuleName","ModuleParent","Pos");
		$value=array($this->ModuleID,$this->ModuleName,$this->ModuleParent,$this->Pos);
		return $ob->insertData($conn,$this->Table,$field,$value);
	}
	function update($conn)
	{
		
		$ob=new ClassConnect();
		$field=array("ModuleID","ModuleName","ModuleParent","Pos");
		$value=array($this->ModuleID,$this->ModuleName,$this->ModuleParent,$this->Pos);
		$where="ModuleID ='".$this->ModuleID."'";
		if($this->ModuleID!=$this->ModuleParent)
			return $ob->updateData($conn,$this->Table,$field,$value,$where);			
	}
	
	function delete($conn)
	{
		$ob=new ClassConnect();
		$listChid=$this->getListChil($this->ModuleID);
		if(count($listChid)==0)
		{
			$where="ModuleID ='".$this->ModuleID."'";
			return $ob->deleteData($conn,$this->Table,$where);
		}
			
	}
	function getListChil($id)
	{
		$data=$this->getTable(" WHERE ModuleParent='$id' ORDER BY Pos");
		$arr=array();
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['ModuleID']);
		return $arr;
	}
	function isLastChid()
	{
		$maxpost=$this->getNextPos($this->ModuleParent);
		if($this->Pos == $maxpost-1)
			return true;
		else 
			return false;
	}
	function getNextPos($cparent)
	{
		$ob=new ClassConnect();
		$sql="SELECT max(Pos) as max FROM `module` where ModuleParent='$cparent'";
		$row=$ob->singleData($sql);
		$max=$row['max'];
		return $max + 1;
	}
	function deep($id)
	{
		if($id=="")
			return 0;
		$count=1;
		$obj=new Module();
		$obj->loadByKey($id);
		
		while($obj->ModuleParent!="")
		{
			$count++;
			$obj->loadByKey($obj->ModuleParent);
		}
		return $count-1;
	}
	function travel($id,&$list)
	{
		array_push($list,$id);
		$arr=$this->getListChil($id);
		if(count($arr)>0)
			for($i=0;$i<count($arr);$i++)
				$this->travel($arr[$i],$list);
		else
			return;
		
	}
	function showtree($id,$usertyeid)
	{
		$ut=new UserType();
		$ut->loadByKey($usertyeid);
		if($id!="" && $ut->isPermission($id) == -1)
		{
			echo "";
		}
		else
		{		
			$obj=new Module();
			$obj->loadByKey($id);
			$listchid=$this->getListChil($id);
			if($id!="")
			{
				if(count($listchid)==0)
					echo "<li><a href='?module=".$obj->ModuleID."'>".$obj->ModuleName."</a></li>";
				else
					echo "<li><a href='#'>".$obj->ModuleName."</a>";
			}
			
			if(count($listchid)>0)
			{
				echo "<ul>";
				for($i=0;$i<count($listchid);$i++)
				{
					$this->showtree($listchid[$i],$usertyeid);
				}
				echo "</ul>";
				if($id!="")
					echo "</li>";
			}	
		}
	}
	function showTreeBylist($list,$root, &$arr)
	{
		
		for($i=0;$i<count($list);$i++)
		{
			$obj=new Module();
			$obj->loadByKey($list[$i]);
			$listchid=$this->getListChil($obj->ModuleID);
			if(arrsearch($arr,$obj->ModuleID) == -1 && $obj->ModuleID!="")
				if($obj->ModuleParent==$root)
				{
					array_push($arr,$obj->ModuleID);
					if(count($listchid)==0)
						echo "<li><a href='?module=".$obj->ModuleID."'>".$obj->ModuleName."</a></li>";
					else
					{
						echo "<li><a href='#'>".$obj->ModuleName."</a><ul>";					
						@$this->showTreeBylist($list,$obj->ModuleID, $arr);
						echo "</ul></li>";
					}
				}	
		}
		
		
	}
	
	function showDeep($id,$letter)
	{
		$deep=$this->deep($id);
		$s="";
		for($i=0;$i<$deep;$i++)
			$s=$s.$letter;
		return $s;
	}
	function cbModule($cbname,$id,$selectvalue)
	{
		$arr=array();
		$this->travel($id,$arr);
		$cb="<select name='$cbname'>";
		for($i=0;$i<count($arr);$i++)
		{
			$obj=new Module();
			$obj->loadByKey($arr[$i]);
			$sel="";
			if($arr[$i]==$selectvalue)
				$sel="selected";
			$cb.="<option value='".$obj->ModuleID."' $sel>".$this->showDeep($obj->ModuleID,'&nbsp;&nbsp;&nbsp;').$obj->ModuleName."</option>";
		}
		$cb.="</select>";
		return $cb;
	}
}
?>
